﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace CapstoneMockup
{
    public partial class Expenses : PhoneApplicationPage
    {
        public Expenses()
        {
            InitializeComponent();
            //get all logs for the user in the database
            ListOfExpenses.ItemsSource = App.ViewModel.User._expenseLogs;
        }

        private void OpenLog_Click(object sender, RoutedEventArgs e)
        {
            if (App.ViewModel.User._currentLog != null)
            {
                App.ViewModel.User.RefreshLogFile(App.ViewModel.User._currentLog);
            }
            try
            {
                string seletectedItem = ListOfExpenses.SelectedItem.ToString();
                foreach (ExpenseLog expenseLog in App.ViewModel.User._expenseLogs)
                {
                    if (expenseLog.ToString().Equals(seletectedItem))
                    {
                        App.ViewModel.User._currentLog = expenseLog;
                    }
                }
            }
            catch (NullReferenceException)
            {

            }

            if (App.ViewModel.User._currentLog != null)
                if (NavigationService.CanGoBack)
                    NavigationService.GoBack();
        }

        private void NewLog_Click(object sender, RoutedEventArgs e)
        {
            if (NewLogNametxtbox.Text != "Put name of new log name here")
            {
                App.ViewModel.User.NewExpenseLog(NewLogNametxtbox.Text);
                NewLogNametxtbox.Text = "New Log created. Open Log to add to it.";
                RefreshListOfExpenses();
            }
            else
            {
                ErrorExpenseLogsTxtBlock.Text = "You must give the log a name.";
            }
        }

        private void NewLogNametxtbox_GotFocus(object sender, RoutedEventArgs e)
        {
            NewLogNametxtbox.Text = "";
        }

        private void RefreshListOfExpenses()
        {
            if (App.ViewModel.User._expenseLogs.Count > 0)
                ListOfExpenses.ItemsSource = App.ViewModel.User._expenseLogs;
        }

        private void Back_Click(object sender, RoutedEventArgs e)
        {
            if (NavigationService.CanGoBack)
                NavigationService.GoBack();
        }
    }
}